package com.example.onlineeducation.ui.auth

import android.content.Context
import android.content.Intent
import android.view.View
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.entity.ProInfoEntity
import com.example.onlineeducation.databinding.LoginConfirmActivityBinding
import com.example.onlineeducation.ui.main.MainActivity
import com.example.onlineeducation.ui.online.OnlineExaminationAc
import com.example.onlineeducation.viewmodel.LoginConfirmVM
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.net.doSuccess

class LoginConfirmActivity : BaseActivity<LoginConfirmActivityBinding>(LoginConfirmActivityBinding::inflate) {
    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, LoginConfirmActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mViewModel: LoginConfirmVM by ycViewModels()

    private var mSelectPro: ProInfoEntity? = null

    private val mSelectProList: OptionsPickerView<ProInfoEntity> by lazy {
        OptionsPickerBuilder(this) { _: Int, _: Int, _: Int, _: View? -> }
            .build<ProInfoEntity?>().apply {
                setItemToContent {
                    (it as ProInfoEntity?)?.proName ?: "无"
                }
                setItemSelectListener { item, _ ->
                    mViewBinding.switchPro(item as ProInfoEntity)
                }
            }
    }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarColor(R.color.transparent)
            .statusBarDarkFont(false)
            .hideBar(BarHide.FLAG_SHOW_BAR)
            .fitsSystemWindows(false)
            .init()
    }

    override fun LoginConfirmActivityBinding.initView() {
        tvUserPro.setOnClickListener {
            mSelectProList.show()
        }
        btnRefresh.setOnClickListener {
            mViewModel.getUerInfo(true)
        }
        btnLogin.setOnClickListener {
            when {
                mSelectPro == null -> {
                    showToast("没有项目,无法登录")
                }
                mSelectPro!!.proStatusIsOK.not() -> {
                    showToast("项目状态异常,无法登录")
                }
                else -> {
                    App.mInstant.mViewModel.refreshProInfo(mSelectPro)
                    MainActivity.newInstance(this@LoginConfirmActivity)
                    finish()
                }
            }
        }
        mViewModel.mUerInfo.observe {
            it.doSuccess {
                tvUserName.text = it.userName
                tvUserSex.text = it.userSexName
                mSelectProList.setPicker(it.userProInfoList)
                switchPro(it.userProInfoList?.get(0))
            }.ycShowNetError()
        }
        mViewModel.getUerInfo(false)
    }

    private fun LoginConfirmActivityBinding.switchPro(proInfo: ProInfoEntity?) {
        mSelectPro = proInfo
        if (mSelectPro != null) {
            tvUserPro.text = mSelectPro!!.proName
            tvProUserWorkType.text = mSelectPro!!.proUserWorkTypeName
            tvProStatus.text = mSelectPro!!.proStatusName
        } else {
            tvUserPro.text = "无"
            tvProUserWorkType.text = "无"
            tvProStatus.text = "无"
            mSelectProList.setPicker(listOf())
        }
    }
}